package com.cryptoneedle.garden.repository.source.metadata;


import com.bubbles.engine.data.core.repository.BaseRepository;
import com.cryptoneedle.garden.entity.source.metadata.SourceColumn;
import com.cryptoneedle.garden.key.ColumnKey;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>description: 源数据源-列 </p>
 *
 * @author CryptoNeedle
 * @date 2025-06-20
 */
@Repository
public interface SourceColumnRepository extends BaseRepository<SourceColumn, ColumnKey> {

    @Query("  FROM SourceColumn " +
            "WHERE catalogName = :catalogName " +
            "ORDER BY catalogName, databaseName, tableName, position, columnName")
    List<SourceColumn> columns(String catalogName);

    @Query("  FROM SourceColumn " +
            "WHERE catalogName = :catalogName " +
            "  AND databaseName = :databaseName " +
            "ORDER BY catalogName, databaseName, tableName, position, columnName")
    List<SourceColumn> columns(String catalogName, String databaseName);

    @Query("  FROM SourceColumn " +
            "WHERE catalogName = :catalogName " +
            "  AND databaseName = :databaseName " +
            "  AND tableName = :tableName " +
            "ORDER BY catalogName, databaseName, tableName, position, columnName")
    List<SourceColumn> columns(String catalogName, String databaseName, String tableName);

    @Query("""
             FROM SourceColumn c
            WHERE c.catalogName = :catalogName
              AND c.databaseName = :databaseName
              AND c.tableName = :tableName
              AND EXISTS (SELECT 1
                            FROM SourceDimension d
                           WHERE c.catalogName = d.catalogName
                             AND c.databaseName = d.databaseName
                             AND c.tableName = d.tableName
                             AND c.columnName = d.columnName
                             AND d.enabled = TRUE)
            ORDER BY c.catalogName, c.databaseName, c.tableName, c.position, c.columnName""")
    List<SourceColumn> columnsWithDimension(String catalogName, String databaseName, String tableName);

    @Query("""
             FROM SourceColumn c
            WHERE c.catalogName = :catalogName
              AND c.databaseName = :databaseName
              AND c.tableName = :tableName
              AND NOT EXISTS (SELECT 1
                                FROM SourceDimension d
                               WHERE c.catalogName = d.catalogName
                                 AND c.databaseName = d.databaseName
                                 AND c.tableName = d.tableName
                                 AND c.columnName = d.columnName
                                 AND d.enabled = TRUE)
            ORDER BY c.catalogName, c.databaseName, c.tableName, c.position, c.columnName""")
    List<SourceColumn> columnsWithoutDimension(String catalogName, String databaseName, String tableName);
}